package com.weng.coder.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.coder.entity.Template;
import com.weng.coder.mapper.TemplateMapper;
import com.weng.framework.dao.mybatis.model.page.PageModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 代码模板 业务实现类
 *
 * @作者 wengzhonghui
 * @创建时间 2018-02-25 15:49:52
 */
@Service("templateService")
public class TemplateServiceImpl  implements TemplateService {
	@Resource
	private TemplateMapper templateMapper;

	public IPage<Template> getDataByPage(int pageNo, int pageSize, Template t) {
		QueryWrapper<Template> qw = new QueryWrapper<>();
		qw.eq("PROJECT_ID", t.getProjectId());
		if(t.getName()!=null) qw.like("NAME", "%"+ t.getName() +"%");
		if(t.getCode()!=null) qw.like("CODE", "%"+ t.getCode() +"%");
		qw.orderByAsc("NAME");
		IPage<Template> page = new PageModel<>(pageNo,pageSize);
		return this.templateMapper.selectPage(page, qw);
	}

	@Override
	public void delete(Template t) {
		this.templateMapper.deleteById(t.getId());
	}

	public Template getOne(String id){
				return templateMapper.selectById(id);
			}

			public List<Template> findByIds(List<String> ids){
				templateMapper.selectBatchIds(ids);
				List<Template> list =  templateMapper.selectList( new QueryWrapper<>());
				return list;
			}

			@Override
			public boolean save(Template t) {
			   if(StringUtils.isEmpty(t.getId())){
						 templateMapper.insert(t);
				 }else{
						 templateMapper.updateById(t);
				 }

				 return true;
			}


		@Override
		public IPage<Template> list(Integer pageNo, Integer pageSize) {
				QueryWrapper<Template> qw = new QueryWrapper<>();
				IPage<Template> page = new PageModel<>(pageNo,pageSize);
				return templateMapper.selectPage(page,qw);
		}
	

			@SuppressWarnings("unchecked")
			public List<Map<String,Object>> getIdsAndNames(String projectId){

					QueryWrapper<Template> qw = new QueryWrapper();
					qw.eq("PROJECT_ID", projectId);
					qw.orderByAsc("NAME");
					List<Template> list = this.templateMapper.selectList(qw);
					List<Map<String,Object>> resultList = new ArrayList<>();
					if(list != null){
							for(Template t : list){
									Map<String,Object> dt = new HashMap<>();
									dt.put("id", t.getId());
									dt.put("name", t.getName());
									resultList.add(dt);
							}
					}
					return resultList;
			}

			public List<String> getTemplateIdsByProjectId(String projectId){
					QueryWrapper<Template> qw = new QueryWrapper();
					qw.eq("PROJECT_ID", projectId);
					List<Template> list = this.templateMapper.selectList(qw);
					List<String> ids = new ArrayList<>();
					if(list != null){
							for(Template t : list){
									ids.add(t.getId());
							}
					}
					return ids;
			}



			@Override
			public int deleteByProjectId(String projectId){
					QueryWrapper<Template>  qw = new QueryWrapper<>();
					qw.eq("PROJECT_ID", projectId);
					return templateMapper.delete(qw);
			}



}
